package com.magnificent.atlantis.client.data.task.planner;

import com.magnificent.atlantis.client.data.task.common.AbstractTask;
import com.magnificent.atlantis.client.data.task.creator.AbstractCreatorTask;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;

/**
 * User: Babanin
 * Date: 04.03.2009
 * Time: 0:53:23
 */
@XmlRootElement(name = "task")
public abstract class AbstractPlannerTask extends AbstractTask implements Comparable<AbstractPlannerTask> {

    private Date endDate;
    private Date startDate;

    private boolean isScheduled;
    private boolean cantBeScheduled;

    public AbstractPlannerTask() {
    }

    public AbstractPlannerTask(AbstractCreatorTask creatorBasicTask) {
        setId(creatorBasicTask.getId());

        setName(creatorBasicTask.getName());
        setDescription(creatorBasicTask.getDescription());

        setDependencyTaskId(creatorBasicTask.getDependencyTaskId());
        setDependencyTaskTime(creatorBasicTask.getDependencyTaskTime());

        setDependencyTasks(creatorBasicTask.getDependencyTasks());
    }

    public boolean isBasicTask() {
        return this instanceof BasicPlannerTask;
    }

    public boolean isMeetingTask() {
        return this instanceof SimpleMeetingPlannerTask;
    }

    public BasicPlannerTask getBasicTask() {
        return (BasicPlannerTask) this;
    }

    public SimpleMeetingPlannerTask getMeetingTask() {
        return (SimpleMeetingPlannerTask) this;
    }

    @XmlAttribute()
    public Date getStartDate() {
        return startDate;
    }

    public void reInitializeStartDate(Date startDate) {
        setStartDate(startDate);
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @XmlAttribute()
    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public boolean isFinished() {
        return isBasicTask() ?
                getBasicTask().getProgress() == 100 :
                getMeetingTask().isCompleted();
    }

    public int compareTo(AbstractPlannerTask o) {
        return startDate.compareTo(o.startDate);
    }

    @XmlAttribute()
    public boolean isScheduled() {
        return isScheduled;
    }

    public void setScheduled(boolean scheduled) {
        isScheduled = scheduled;
    }

    @XmlAttribute()
    public boolean isCantBeScheduled() {
        return cantBeScheduled;
    }

    public void setCantBeScheduled(boolean cantBeScheduled) {
        this.cantBeScheduled = cantBeScheduled;
    }
}
